package tt.dz.mvc.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.service.base.ServiceSplitratioService;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

/**
 * 
 * @author pengyu
 *
 */
@Controller
@RequestMapping("/splitratioApi")
public class ServiceSplitratioAction extends BaseAction
{
	
	@Autowired
	private ServiceSplitratioService splitratioServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		String create_oper_id = (String) session.getAttribute("login_oper_id"); // 更新人id
		
		String[] entityTypes = StringUtil.request2Array(request, "entityTypes");
		String[] entityIds   = StringUtil.request2Array(request, "entityIds");
		String[] entityNames = StringUtil.request2Array(request, "entityNames");
//		String[] chargeIds   = StringUtil.request2Array(request, "chargeIds");
		String[] ratios      = StringUtil.request2Array(request, "ratios");
		String[] minAmount = StringUtil.request2Array(request, "minAmounts");
		
		String serviceNo = request.getParameter("serviceNo");
		String schemeName = request.getParameter("schemeName");
		//只需要  serviceNo  schemeName
		return splitratioServiceImpl.add(entityTypes, entityIds,entityNames,serviceNo, ratios, minAmount, schemeName, create_oper_id);
	}
//	//新增一个方案
//	@RequestMapping(value="add",method=RequestMethod.POST)
//	@ResponseBody
//	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
//	{
//		String serviceNo = request.getParameter("serviceNo");
//		String schemeName = request.getParameter("schemeName");
//		return splitratioServiceImpl.add(serviceNo,schemeName);
//	}	
//	//完成一个方案，把实体和对应比例都匹配
//	@RequestMapping(value="add",method=RequestMethod.POST)
//	@ResponseBody
//	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
//	{
//		String serviceNo = request.getParameter("serviceNo");
//		String schemeName = request.getParameter("schemeName");
//		return splitratioServiceImpl.add(serviceNo,schemeName);
//	}
		
		
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> update(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		String update_oper_id = (String) session.getAttribute("login_oper_id"); // 更新人id
		
		String[] entityTypes = StringUtil.request2Array(request, "entityTypes");
		String[] entityIds   = StringUtil.request2Array(request, "entityIds");
		String[] entityNames = StringUtil.request2Array(request, "entityNames");
//		String[] chargeIds   = StringUtil.request2Array(request, "chargeIds");
		String[] ratios      = StringUtil.request2Array(request, "ratios");
		String[] minAmount = StringUtil.request2Array(request, "minAmounts");
		String serviceNo = request.getParameter("serviceNo");   
		String schemeName = request.getParameter("schemeName");
		String schemeNo = request.getParameter("schemeNo");
		Integer valid_flag = Integer.valueOf(request.getParameter("valid_flag"));
//		return splitratioServiceImpl.update(entityTypes, entityIds,entityNames, serviceNo, chargeIds, ratios, minAmount, schemeName,schemeNo);
		return splitratioServiceImpl.update(entityTypes, entityIds,entityNames, ratios, minAmount, serviceNo, schemeName, schemeNo,valid_flag, update_oper_id);
	}
	
	
	@RequestMapping(value="bindChargeIds",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> bindChargeIds(HttpServletRequest request, HttpServletResponse response)
	{
		String[] chargeIdArray =   StringUtil.request2Array(request, "chargerIds");
		String serviceNo = request.getParameter("serviceNo");
		String schemeNo = request.getParameter("schemeNo");
		splitratioServiceImpl.bindChargeIds(serviceNo, chargeIdArray, schemeNo);
		return new JsonCommon<>(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
	
	@RequestMapping(value="delete/{schemeNo}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(	@PathVariable(value="schemeNo") String schemeNo,HttpServletRequest request, HttpServletResponse response)
	{
		return splitratioServiceImpl.delete(schemeNo);
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		return splitratioServiceImpl.findPageObjs(this.buildQueryParam(request, true));
	}
	
	@RequestMapping(value="findSchemeByChargerId/{chargerId}",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findSchemeByChargerId(@PathVariable(value="chargerId") String chargerId,HttpServletRequest request, HttpServletResponse response)
	{
		return splitratioServiceImpl.findSchemeByChargerId(chargerId);
	}
	
	@RequestMapping(value="findScheme/{schemeNo}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findScheme(@PathVariable(value="schemeNo") String schemeNo,HttpServletRequest request, HttpServletResponse response)
	{
		return splitratioServiceImpl.findSchemeBySchemeNo(schemeNo);
	}
	
	@RequestMapping(value="findChargerBySchemeNo",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findChargerBySchemeNo(HttpServletRequest request, HttpServletResponse response)
	{
		String cityCode = request.getParameter("cityCode");
		String schemeNo = request.getParameter("schemeNo");
		String serviceNo = request.getParameter("serviceNo");
		String likeValue = request.getParameter("likeValue");
		return splitratioServiceImpl.findChargerBySchemeNo(schemeNo, serviceNo,cityCode, likeValue);
	}
	
	@RequestMapping(value="getUnbindCharges",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> getUnbindCharges(HttpServletRequest request, HttpServletResponse response)
	{
		String serviceNo = request.getParameter("serviceNo");
		String schemeNo = request.getParameter("schemeNo");
		if(StringUtil.isBlank(serviceNo)){
			 return new JsonForList<>(null, "10303101", "缺少参数字段");
		  }
		return splitratioServiceImpl.getUnbindCharges(serviceNo,schemeNo);
	}
	
	@RequestMapping(value="unBind",method=RequestMethod.POST)
	@ResponseBody
	public JsonForList<?> unBind(HttpServletRequest request, HttpServletResponse response)
	{
		String serviceNo = request.getParameter("serviceNo");
		String schemeNo = request.getParameter("schemeNo");
		String chargerIds = request.getParameter("chargerIds");
		if(StringUtil.isBlank(chargerIds)){
			return new JsonForList<>(null, "10303101", "缺少参数字段");
		}
		List<String> chargeIdList =  StringUtil.request2List(request, "chargerIds");
		return splitratioServiceImpl.unBind(chargeIdList,serviceNo,schemeNo);
	}

}
